﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;

namespace EnterpriseFund.Controllers
{
    [OutputCache(Duration = 0, VaryByParam = "none")]   
    public class EnterprisePropController : AdminBaseController
    {
        //
        // GET: /FinanceProp/
        /// <summary>
        /// 代码维护主页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_EnterpriseProp", Order = 1)]
        [LogPageActionFilter("企业性质管理", Order = 2)]
        public ActionResult Index()
        {
            List<Model.EnterpriseProp> codeNameList = new List<Model.EnterpriseProp>();
            ViewData["blcode"] = true;
            BLL.EnterpriseProp codeNameBll = new BLL.EnterpriseProp();
            codeNameList = codeNameBll.GetModelList("");
            return View(codeNameList);
        }

        /// <summary>
        /// 编码树相关调用方法 主要用于刷新
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult EnterprisePropListPartial()
        {
            BLL.EnterpriseProp codeNameBll = new BLL.EnterpriseProp();
            List<Model.EnterpriseProp> codeNameList = new List<Model.EnterpriseProp>();
            codeNameList = codeNameBll.GetModelList("");
            ViewData["blcode"] = true;
            return PartialView("EnterprisePropListPartial", codeNameList);
        }

        /// <summary>
        /// 编码增加修改
        /// </summary>
        /// <param name="codeClsNo">编码类别编码 必须有具体值</param>
        /// <param name="codeNo">编码</param>
        /// <returns>视图</returns>
        public ActionResult EnterprisePropEdit(string fEnterprisePropNo)
        {
            Model.EnterpriseProp codeNameObj = new Model.EnterpriseProp();
            ViewData["blcodeNo"] = true;
            ViewData["enterpriseFinancePropList"] = new List<Model.EnterpriseFinanceProp>();
            if (null != fEnterprisePropNo)
            {
                fEnterprisePropNo = Fetch.estimateString(fEnterprisePropNo, CheckGetEnum.Safety);
                BLL.EnterpriseProp codeNameBll = new BLL.EnterpriseProp();
                codeNameObj = codeNameBll.GetModel(fEnterprisePropNo);
                if (null == codeNameObj) 
                {
                    codeNameObj = new Model.EnterpriseProp();
                }
                else
                {
                    BLL.EnterpriseFinanceProp enterpriseFinancePropBLL = new BLL.EnterpriseFinanceProp();
                    ViewData["enterpriseFinancePropList"] = enterpriseFinancePropBLL.GetModelList("FEnterprisePropNo='" + fEnterprisePropNo + "' and FYear=" + OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                    ViewData["blcodeNo"] = false;
                }
            }

            BLL.FinanceProp financePropBLL = new BLL.FinanceProp();
            ViewData["financePropList"] = financePropBLL.GetModelList("");
            return View(codeNameObj);
        }


        /// <summary>
        /// 编码保存
        /// </summary>
        /// <param name="codeNo">编码编号</param>
        /// <param name="codeName">编码名字</param>
        /// <param name="codeClsNo">编码类别编码</param>
        /// <param name="isAdd">是否添加true添加，false修改</param>
        /// <returns>是否添加成功</returns>
        public string EnterprisePropSave(string fEnterprisePropNo, string fEnterprisePropName, string fIsByTaxFinanceProp, bool isAdd)
        { 
            string str = string.Empty;

            fEnterprisePropNo = Fetch.estimateString(fEnterprisePropNo, CheckGetEnum.Safety);
            fEnterprisePropName = Fetch.estimateString(fEnterprisePropName, CheckGetEnum.Safety);

            //string[] array = financeProplist.Split(new string[] { "%!^" }, StringSplitOptions.RemoveEmptyEntries);

            List<Model.EnterpriseFinanceProp> enterpriseFinancePropList = new List<Model.EnterpriseFinanceProp>();
            //foreach (string arr in array)
            //{
            //    Model.EnterpriseFinanceProp model = new Model.EnterpriseFinanceProp();
            //    model.FFinancePropNo = arr;
            //    model.FEnterprisePropNo = fEnterprisePropNo;
            //    model.FYear = decimal.Parse(OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR));
            //    enterpriseFinancePropList.Add(model);
            //}

            BLL.EnterpriseProp codeNameBll = new BLL.EnterpriseProp();
            if (isAdd)//增加时判断编码是否已存在
            {
                if (codeNameBll.Exists(fEnterprisePropNo))
                {
                    return "exists";
                }
            }

            Model.EnterpriseProp codeNameObj = new Model.EnterpriseProp();
            codeNameObj.FEnterprisePropNo = fEnterprisePropNo;
            codeNameObj.FEnterprisePropName = fEnterprisePropName;
            codeNameObj.FIsByTaxFinanceProp = fIsByTaxFinanceProp;
            codeNameObj.FYear = decimal.Parse(OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR));

            try
            {
                if (isAdd)
                {
                    codeNameBll.Add(codeNameObj, enterpriseFinancePropList);
                }
                else
                {
                    codeNameBll.Update(codeNameObj, enterpriseFinancePropList);
                }
                str = "ok";

            }
            catch (Exception)
            {

                str = "error";
            }

            return str;
        }


        /// <summary>
        /// 删除编码数据
        /// </summary>
        /// <param name="codeNo">编码编号</param>
        /// <param name="codeClsNO">编码类别编码</param>
        /// <returns>是否删除成功</returns>
        public string DelEnterpriseProp(string fEnterprisePropNo)
        {
            string str = string.Empty;
            if (null != fEnterprisePropNo)
            {
                fEnterprisePropNo = Fetch.estimateString(fEnterprisePropNo, CheckGetEnum.Safety);
                BLL.EnterpriseProp codeNameBll = new BLL.EnterpriseProp();
                try
                {
                    codeNameBll.Delete(fEnterprisePropNo);
                    str = "ok";
                }
                catch (Exception)
                {
                    str = "error";
                }
            }
            return str;
        }

    }
}
